import 'package:flutter/material.dart';
import 'package:flutter_plugin_tts/flutter_plugin_tts.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/state/user_role.dart';
import 'package:ytbusiness/utils/sp_util.dart';
import 'package:ytbusiness/utils/asset_util.dart';
import '../routes.dart';

///初始化 app
///
/// 1.双击退出
/// 2.初始化tts
/// 3.初始化缓存（判断要去那个 页面）
///
class InitApp extends StatefulWidget {
  @override
  _InitAppState createState() => _InitAppState();
}

class _InitAppState extends State<InitApp> {
  DateTime _lastPressedAt; //上次点击时间
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    FlutterPluginTts.setLanguage('zh-CN').then((v) {
      print("设置中文语音");
    });
    _initCache();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance =
        ScreenUtil(width: 375, height: 667, allowFontScaling: false)
          ..init(context);
    return WillPopScope(
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt) > Duration(seconds: 1)) {
          //两次点击间隔超过1秒则重新计时
          _lastPressedAt = DateTime.now();
          return false;
        }
        return true;
      },
      child: Container(
        color: Colors.white,
        width: double.infinity,
        height: double.infinity,
        child: Image.asset(
          AssetUtil.image("img_launch"),
          // "assets/images/startup.gif",
          fit: BoxFit.fill,
        ),
      ),
      //CheckRole(),
    );
  }

  ///初始化缓存  --- 启动速度较慢---白屏
  _initCache() async {
    ///设置主题色
    YTTheme.getInstance()
      ..color = Color(0xff37C2BB)
      ..init();

    ///null加载 1跳过
    if (await SPUtil.getInt(RouteManager.splash) == null) {
      await Navigator.of(context).pushReplacementNamed(RouteManager.splash);
      return;
    }

    ///null
    await Navigator.of(context).pushReplacementNamed(RouteManager.adPage);

    // /// 获取用户角色
    // int role = await SPUtil.getInt('role');
    // print("用戶角色:$role");
    //
    // ///初始化用户信息
    // if (role != null) {
    //   Provider.of<UserRole>(context, listen: false).upDateUserRole(role);
    //   print("------>:${Provider.of<UserRole>(context, listen: false).role}");
    // } else {
    //   role = Provider.of<UserRole>(context, listen: false).role;
    // }
    // ///根据用户登录状态跳转到相应页面
    // if (role != null) {
    //   switch (role) {
    //     case 1:
    //       YTTheme.getInstance()
    //         ..color = Color(0xff00C65F)
    //         ..init();
    //       await Navigator.of(context).pushReplacementNamed("check_role");
    //       //Navigator.of(context).pushReplacementNamed("create_store");
    //       //jump(context, widget: RepairFactoryHome(),
    //       // name: "repair_factory_home");
    //       break;
    //     case 2:
    //       YTTheme.getInstance()
    //         ..color = Color(0xff37C2BB)
    //         ..init();
    //       await Navigator.of(context).pushReplacementNamed("check_role");
    //       //Navigator.of(context).pushReplacementNamed("check_role");
    //       // urlPush(context, "yuntu://create_store");
    //       break;
    //     case 3:
    //       YTTheme.getInstance()
    //         ..color = Color(0xff00D5FF)
    //         ..init();
    //       await Navigator.of(context).pushReplacementNamed("scout");
    //       //  urlPush(context, "yuntu://check_role");
    //       break;
    //   }
    // }
  }
}
